package com.br.negocio.dao;


import java.util.Collections;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.br.negocio.vo.CompraVO;
import com.br.negocio.vo.ServicoCompraVO;

@Service("compraDAO")
public class CompraDAO {

        @PersistenceContext
        private EntityManager entityManager;

        
        public void inserirCompra(CompraVO compraVO) {
                try {
                        entityManager.persist(compraVO);
                        
                        for ( ServicoCompraVO ServicoCompra :  compraVO.getListaServicoCompraVO() ){
                                ServicoCompra.setCompraVO(compraVO);
                                ServicoCompra.getServicoCompraPK().setCodCompra(compraVO.getCodigo());
                                ServicoCompra.getServicoCompraPK().setCodservico(ServicoCompra.getServicoVO().getCodigo());
                                entityManager.persist(ServicoCompra);
                        }
                        
                } catch (Exception e) {
                        throw new RuntimeException("Erro ao inserir compra: " + e.getMessage(), e);
                }
        }
        
        @Transactional(propagation = Propagation.REQUIRED)
        public void atualizarCompra(CompraVO compraVO) {
                try {
                        entityManager.merge(compraVO);
                } catch (Exception e) {
                        throw new RuntimeException("Erro ao atualizar compra: " + e.getMessage(), e);
                }
        }

        

        @SuppressWarnings("unchecked")
        @Transactional(propagation = Propagation.REQUIRED)
        public CompraVO obterCompra(CompraVO compraVO) {
                try {
                        CompraVO compraAux = new CompraVO();
                        compraAux = entityManager.find(CompraVO.class, compraVO.getCodigo());
                        /*
                        List<ServicoCompraVO> listaPc = new ArrayList<ServicoCompraVO>();
                        listaPc.addAll((List)compraAux.getListaServicoCompraVO());
                        compraAux.setListaServicoCompraVO(new ArrayList<ServicoCompraVO>());
                        compraAux.setListaServicoCompraVO(listaPc);
                        */
                        Query query = entityManager.createNamedQuery("loadAllServicoCompra");
                        query.setParameter("codigo", compraAux.getCodigo());
                        
//                      //List<ServicoCompraVO> lista = (List<ServicoCompraVO>) entityManager.createNamedQuery("loadAllServicoCompra").getResultList();
                        List<ServicoCompraVO> lista = (List<ServicoCompraVO>) query.getResultList();
                        if ((lista != null) && (!lista.isEmpty())) {
                                compraAux.setListaServicoCompraVO(lista);
                        } else {
                                compraAux.setListaServicoCompraVO(Collections.EMPTY_LIST);
                        }
                        
                        
                        return compraAux;
                        
                } catch (Exception e) {
                        throw new RuntimeException("Erro ao buscar compra: " + e.getMessage(), e);
                }
        }

        @SuppressWarnings("unchecked")
        @Transactional(propagation = Propagation.REQUIRED)
        public List<CompraVO> obterListaCompra() {
                try {
                        List<CompraVO> lista = (List<CompraVO>) entityManager.createNamedQuery("loadAllCompra").getResultList();
                        if ((lista != null) && (!lista.isEmpty())) {
                                return lista;
                        } else {
                                return Collections.EMPTY_LIST;
                        }
                } catch (Exception e) {
                        throw new RuntimeException("Erro ao carregar todos os compras.", e);
                }
        }
        
        @SuppressWarnings("unchecked")
        @Transactional(propagation = Propagation.REQUIRED)
        public boolean validaDuplicado(CompraVO compraVO) {
                Query query = entityManager.createNamedQuery("findDuplicatedCompra");
                query.setParameter("codigo", compraVO.getCodigo());
                query.setParameter("nota", compraVO.getNota());
                query.setParameter("notaSerie", compraVO.getNotaSerie());
                List lista = query.getResultList();
                if (lista.isEmpty()) {
                        return true;
                } else {
                        return false;
                }
        }

        
        
        public void setEntityManager(EntityManager entityManager) {
                this.entityManager = entityManager;
        }

}
